package com.runssnail.demo.springboot;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.Filter;

/**
 * Hello world!
 */
@SpringBootApplication
@Controller
public class App {

    @RequestMapping("/")
    public String index() {
        return "index";
    }

    @RequestMapping("index.htm")
    public String greeting() {
        return "index";
    }

    @RequestMapping("/product/index.htm")
    public String productIndex() {
        return "product_index";
    }

    /**
     * 注册filter
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean registrationHelloWorldFilter(@Qualifier("helloWorldFilter") Filter helloWorldFilter) {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
//        HelloWorldFilter filter = new HelloWorldFilter();
        registrationBean.setFilter(helloWorldFilter);
        registrationBean.addUrlPatterns("/product/*"); // 设置匹配模式
        registrationBean.setOrder(Integer.MAX_VALUE); // 设置排序字段
        return registrationBean;
    }

    /**
     * 注册filter
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean registrationIndexFilter() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        IndexFilter filter = new IndexFilter();
        registrationBean.setFilter(filter);
        registrationBean.setOrder(1);
        return registrationBean;
    }

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}
